package com.hz.pay.center.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.hz.pay.center.exception.BizException;
import com.pay.common.enums.PayStatusEnum;
import com.hz.pay.center.mapper.PaySerialInfoMapper;
import com.hz.pay.center.model.PaySerialInfo;
import com.hz.pay.center.service.IPaySerialService;
import com.pay.common.req.NetBankPayReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 支付流水相关接口实现类
 *  日期
 *  作者
 */
@Service
public class PaySerialServiceImpl implements IPaySerialService {


    @Autowired
    private PaySerialInfoMapper paySerialInfoMapper;

    /**
     * 创建支付流水
     * @param netBankPayReq
     */
    @Override
    public PaySerialInfo createPaySerial(NetBankPayReq netBankPayReq) throws BizException {

        PaySerialInfo paySerialInfo = new PaySerialInfo();

        //单机锁。 分布式锁
      synchronized (this){
          List<PaySerialInfo> paySerialInfoList = paySerialInfoMapper.queryByOrderId(netBankPayReq.getOrderId());

          if (!CollectionUtils.isEmpty(paySerialInfoList)){
              throw new BizException("该订单正在支付中");
          }
      }



        paySerialInfo.setDeviceInfo(""); //TODO
        paySerialInfo.setGoodsDetail(""); //TODO
        paySerialInfo.setClientIp(""); //TODO
        paySerialInfo.setCreateBy("system");
        paySerialInfo.setCreateTime(new Date());

        paySerialInfo.setMchId(netBankPayReq.getMchId());
        paySerialInfo.setOrderDesc(netBankPayReq.getOrderDesc());
        paySerialInfo.setOrderId(netBankPayReq.getOrderId());
        paySerialInfo.setPayAmount(new BigDecimal(netBankPayReq.getOrderAmt()));
        paySerialInfo.setPayChannel(netBankPayReq.getPayChannel());
        paySerialInfo.setPayWay(netBankPayReq.getPayWay());
        paySerialInfo.setPayStatus(PayStatusEnum.UN_PAY.getCode());

        //https://zhuanlan.zhihu.com/p/107939861
        Snowflake snowflake = new Snowflake(0,0);

        paySerialInfo.setPaySerialNo(snowflake.nextIdStr()); //全局唯一id， 不能重复。 uuid   分布式唯一id 生成方案。

        //雪花算法
        paySerialInfoMapper.insertSelective(paySerialInfo);

        return paySerialInfo;
    }

    @Override
    public PaySerialInfo queryByPaySerialNo(String paySerialNo) {

        return paySerialInfoMapper.queryByPaySerialNo(paySerialNo);
    }



    /**
     * 更新支付流水
     */
    @Override
    public void updateTrade(PaySerialInfo paySerialInfo) {

        paySerialInfoMapper.updateByPrimaryKeySelective(paySerialInfo);


    }

    @Override
    public List<PaySerialInfo> queryAllPollingList(String payChannel) {

        //        // select * from tb_pay_serials  where pay_channel =  payChannel and pay_status in (1,2)
        return new ArrayList<PaySerialInfo>();
    }

    @Override
    public PaySerialInfo queryByOrderId(String orderId) {

        //select * from tb_pay_serials where orderId = orderId and status =0

        return new PaySerialInfo();
    }

    public static void main(String[] args) {
        Snowflake snowflake = new Snowflake(0,0);
        System.out.println(snowflake.nextIdStr());

    }
}
